import {
	doPay
} from '@/api/order/index.js'

export default class PayUtil {



	constructor({
		pay_type,
		order_number,
		order_type,
		pay_way,
		bank_id,
		returnurl,
		order_id
	}) {
		this.pay_type = pay_type;
		this.order_number = order_number;
		this.order_type = order_type;
		this.pay_way = pay_way;
		this.bank_id = bank_id;
		this.order_id = order_id;
		if (returnurl) {
			this.returnurl = returnurl;
		} else {
			let path = `/pages/order/defaultDetail?id=${order_id}`
			// #ifdef H5
			this.returnurl = encodeURIComponent(`${window.location.origin}/#${path}`)
			// #endif
			// #ifdef APP-PLUS
			this.returnurl = encodeURIComponent(`newboomnft://toPage=${encodeURIComponent(path)}`)
			// #endif
		}
		this.doPay({
			order_id,
			order_type
		});
	}

	doPay({
		order_id,
		order_type
	}) {
		let payMethod = this.getPayMethod();
		uni.showLoading({
			title: "支付中"
		})
		doPay({
			pay_type: this.pay_type,
			order_number: this.order_number,
			order_type: this.order_type,
			pay_way: this.pay_way,
			pay_scene: this.getPlat(),
			returnurl: this.returnurl,
			pay_password: this.pay_password,
		}).then(res => {
			if (res.data.balancePay) {
				uni.showToast({
					title: res.msg,
					success() {
						setTimeout(() => {
							uni.redirectTo({
								url: `/pages/order/defaultDetail?id=${order_id}&orderType=${order_type}`
							})
						}, 1500)
					}
				})

			} else {
				payMethod(res.data);
			}
		})
	}

	getPayMethod() {
		let map = {
			1: {},
			2: {},
			3: {},
			4: {
				APP: {
					huifu: this.huifuPay,
					yibao: this.yibao
				},
				H5: {
					huifu: this.huifuPay,
					yibao: this.yibao
				}
			}
		}
		return map[this.pay_type][this.getPlat()][this.pay_way]
	}
	huifuPay(data) {
		
		// #ifdef H5
		window.location.href = data.pay.pay_url
		// #endif
		// #ifdef APP-PLUS
		// plus.runtime.openURL(data.pay.url)
		// plus.runtime.openURL(encodeURI(data.pay.url))
		// window.location.href = data.pay.pay_url
		if (uni.getSystemInfoSync().platform == "ios") {
			// plus.runtime.openURL(encodeURI(data.pay.pay_url))
			plus.runtime.openWeb(data.pay.pay_url)
		}

		if (uni.getSystemInfoSync().platform == "android") {
			// console.log('app支付打印',data.pay.pay_url);
			plus.runtime.openWeb(data.pay.pay_url)
		}

		// #endif
	}

	yibao(data) {
		// #ifdef H5
		window.location.href = data.pay.url
		// #endif
		// #ifdef APP-PLUS
		// window.location.href = data.pay.url
		uni.setStorage({
			key: 'url',
			data: encodeURI(data.pay.url),
			success() {
				uni.navigateTo({
					url: '/pages/webview/webview-storage'
				})
			}
		})
		// #endif
	}

	getPlat() {
		//#ifdef H5
		return "H5"
		//#endif
		//#ifdef APP
		return 'APP'
		//#endif
	}
}